package com.tedu.weibo.controller;

import com.tedu.weibo.common.result.JsonResult;
import com.tedu.weibo.common.result.StatusCode;
import com.tedu.weibo.mapper.WeiboMapper;
import com.tedu.weibo.pojo.dto.WeiboDTO;
import com.tedu.weibo.pojo.entity.User;
import com.tedu.weibo.pojo.entity.Weibo;
import com.tedu.weibo.pojo.vo.WeiboDetailVO;
import com.tedu.weibo.pojo.vo.WeiboVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Api(tags = "微博模块")
@RestController
@RequestMapping("/v1/weibo")
@Slf4j
public class WeiboController {
    @Autowired
    private WeiboMapper weiboMapper;

    @ApiOperation("发表微博")
    @PostMapping("/insert")
    public JsonResult insert(@RequestBody WeiboDTO weiboDTO , @ApiIgnore HttpSession session){
        log.info("WeiboController.insert(),发表微博");
        log.trace("开始执行insert方法");
        log.debug("weiboDTO:"+weiboDTO);//记录程序处理结果,根据它辅助排查逻辑是否有误
        User user=(User) session.getAttribute("user");
        log.trace("通过session获取用户登陆信息");
        log.debug("user:"+user);
        if (user==null){
            log.trace("user为null,未登录");
            return new JsonResult(StatusCode.NOT_LOGIN);
        }else {
            log.trace("user不为null,处理发表微博");
            Weibo weibo=new Weibo();
            weibo.setContent(weiboDTO.getContent());
            weibo.setCreated(new Date());
            weibo.setUserId(user.getId());
            log.debug("weibo:"+weibo);
            weiboMapper.insert(weibo);
            log.trace("插入微博完毕");
            return JsonResult.success();
            //return new JsonResult(StatusCode.OPERATION_SUCCESS);
        }
    }

    @ApiOperation("获取首页微博列表功能")
    @GetMapping("/selectIndex")
    public JsonResult selectIndex(){
        return JsonResult.success(weiboMapper.selectIndex());
        //return new JsonResult(StatusCode.OPERATION_SUCCESS,weiboMapper.selectIndex());
    }

    @ApiOperation("根据ID查询微博")
    @ApiImplicitParam(name = "id",value = "微博的ID",required = true,dataType = "int",example = "1")
    @GetMapping("/selectById")
    public JsonResult selectWeibo(int id){
        return JsonResult.success(weiboMapper.selectById(id));
       // return new JsonResult(StatusCode.OPERATION_SUCCESS,weiboMapper.selectById(id));
    }

   // @ApiOperation("")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "微博的ID",required = true,dataType = "int",example = "1"),
            @ApiImplicitParam(name="name",value = "用户昵称",required = true,dataType = "String",example = "张三")
    })
    @GetMapping("/selectByIdAndName")
    public WeiboDetailVO selectWeiboByIdAndName(int id ,String name){
        return weiboMapper.selectById(id);
    }


}
